﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Person_Person : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        lbAddMsg.Visible = false;
    }



    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        Person person = new Person();
        person.cellphone = txtCellPhone.Text;
        person.firstname = txtFirstName.Text;
        person.person_ID_no = txtIDNo.Text;
        person.surname = txtSurname.Text;
        person.telephone = txtTelephone.Text;

        PersonDBConnection personConn = new PersonDBConnection();
        personConn.update(person);

        setEditableFalse();
        btnUpdate.Visible = false;
        btnCancle.Visible = false;
        btnEdit.Visible = true;
        btnDelete.Visible = true;

        setEditableFalse();
    }
    protected void btnCancle_Click(object sender, EventArgs e)
    {
        setEditableFalse();
        btnUpdate.Visible = false;
        btnCancle.Visible = false;
        btnEdit.Visible = true;
        btnDelete.Visible = true;

        setEditableFalse();
    }
    protected void btnEdit_Click(object sender, EventArgs e)
    {
        setEditableTrue();
        btnUpdate.Visible = true;
        btnCancle.Visible = true;
        btnEdit.Visible = false;
        btnDelete.Visible = false;

        setEditableTrue();
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        PersonDBConnection personConn = new PersonDBConnection();
        Person person = personConn.getPersonByIDno(txtIDNo.Text);
        personConn.delete(person);
    }
    private void setEditableFalse()
    {
        txtIDNo.ReadOnly = true;
        txtFirstName.ReadOnly = true;
        txtSurname.ReadOnly = true;
        txtTelephone.ReadOnly = true;
        txtCellPhone.ReadOnly = true;

        txtIDNo.BorderStyle = BorderStyle.None;
        txtFirstName.BorderStyle = BorderStyle.None;
        txtSurname.BorderStyle = BorderStyle.None;
        txtTelephone.BorderStyle = BorderStyle.None;
        txtCellPhone.BorderStyle = BorderStyle.None;
    }
    private void setEditableTrue()
    {
        txtIDNo.ReadOnly = false;
        txtFirstName.ReadOnly = false;
        txtSurname.ReadOnly = false;
        txtTelephone.ReadOnly = false;
        txtCellPhone.ReadOnly = false;

        txtIDNo.BorderStyle = BorderStyle.NotSet;
        txtFirstName.BorderStyle = BorderStyle.NotSet;
        txtSurname.BorderStyle = BorderStyle.NotSet;
        txtTelephone.BorderStyle = BorderStyle.NotSet;
        txtCellPhone.BorderStyle = BorderStyle.NotSet;
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        if (pnlAdd.Visible)
            pnlAdd.Visible = false;
        else
        {
            pnlAdd.Visible = true;
            setEditableTrue();
        }
    }
    protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        GridViewRow row = GridView1.Rows[e.NewSelectedIndex];

        Session["Software_ID"] = row.Cells[1].Text;

        PersonDBConnection personConn = new PersonDBConnection();
        Person person = personConn.getPersonByIDno(row.Cells[1].Text);
        //personConn.delete(person);

        txtIDNo.Text = person.person_ID_no;
        txtCellPhone.Text = person.cellphone;
        txtFirstName.Text = person.firstname;
        txtSurname.Text = person.surname;
        txtTelephone.Text = person.telephone;

        pnlAdd.Visible = true;


    }
    protected void btnAdd_Click1(object sender, EventArgs e)
    {
        Person person = new Person();
        person.cellphone = txtCellPhone.Text;
        person.firstname = txtFirstName.Text;
        person.person_ID_no = txtIDNo.Text;
        person.surname = txtSurname.Text;
        person.telephone = txtTelephone.Text;

        PersonDBConnection personConn = new PersonDBConnection();
        personConn.insert(person);
        
        txtCellPhone.Text = "";
        txtFirstName.Text = "";
        txtIDNo.Text = "";
        txtSurname.Text = "";
        txtTelephone.Text = "";

        lbAddMsg.Visible = true;

        GridView1.DataBind();
    }


}